#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#include <alloca.h>

extern void afunc(void );

extern etext,edata,end;

int bss_var;
int data_var = 42;
#define SHW_ADR(ID, I) printf("the %8s\t is at adr: %8x\n",ID,&I);

int main(int argc,char * argv[])
{
	char *p,*b,*nb;
	printf("Adr atext: %8x\t Adr edata: %8x\t Adr end: %8x\t\n", &etext, &edata, &end);
	printf("\ntext Location:\n");
	SHW_ADR("main", main);				//address of function 'main' in code segment 
	SHW_ADR("afunc", afunc);			//address of function 'afunc' in code segment
	printf("\nbss Location:\n");
	SHW_ADR("bss_var", bss_var);		//address of variant in BSS segment
	printf("\ndata Location:\n");
	SHW_ADR("data_var", data_var);		//address of variant in data segment
	printf("\nStack Location:\n");
	afunc();
	p = (char*)malloc(32 * sizeof(char));	//assign space in stack segment
	if(p != NULL) {
		SHW_ADR("start", p);			//address of the start of stack segment
		SHW_ADR("end", p + 31);			//address of the end of stack segemtn
	}
	b = (char*)malloc(32 * sizeof(char));	//assign space in heap segment
	nb = (char*)malloc(16 * sizeof(char));	//assign space in heap segment
	printf("\nHeap Locations:\n");
	printf("the Heap start %p\n", b	);							//address of the start of heap segment
	printf("the Heap end : %p\n",(nb + 16 * sizeof(char)));		//address of the end of heap segment
	printf("\nb and nb is Stack\n");	
	SHW_ADR("b", b);		//address of variant 'b' in stack						
	SHW_ADR("nb", nb);		//address of variant 'nb' in stack
	free(b);				//free the space
	free(nb);				//free the space	

}
